package exttable.analysiscreation;

import exttable.view.ViewAnalysis;
import exttable.view.ViewAnalysisParameter;
import exttable.view.ViewSize;
import exttable.view.ViewUnits;

import javax.swing.event.TreeModelEvent;
import javax.swing.event.TreeModelListener;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;
import java.util.Vector;

/**
 * Created by IntelliJ IDEA.
 * User: alex
 * Date: 5/8/11
 * Time: 9:51 PM
 * To change this template use File | Settings | File Templates.
 */
public class Model implements TreeModel {
    private Vector<TreeModelListener> treeModelListeners = new Vector<TreeModelListener>();
    private ViewAnalysis analysis;


    public Model(ViewAnalysis analysis, boolean update) {
        this.analysis = analysis;
        if (update) {
            fireTreeStructureChanged();
        }
    }

    private void fireTreeStructureChanged() {
//        int len = treeModelListeners.size();
        TreeModelEvent e = new TreeModelEvent(this, new Object[]{analysis});
        for (TreeModelListener tml : treeModelListeners) {
            tml.treeStructureChanged(e);
        }

    }

    @Override
    public Object getRoot() {
        return analysis;
    }

    @Override
    public Object getChild(Object parent, int index) {
        if (parent instanceof ViewAnalysis) {
            ViewAnalysis va = (ViewAnalysis) parent;
            return va.getParameters().get(index);
        } else if (parent instanceof ViewAnalysisParameter) {

            ViewAnalysisParameter vap = (ViewAnalysisParameter) parent;
            if (index == 0) {
                return vap.getUnit();
            } else if (index == 1) {
                return vap.getSize();
            } else if (index == 2) {
                return vap.getTypeVariable();
            } else {
                return null;
            }
        } else {
            return "no data";
        }
    }

    @Override
    public int getChildCount(Object parent) {
        int toReturn;
        if (parent instanceof ViewAnalysis) {
            toReturn = ((ViewAnalysis) parent).getParameters().size();
        } else if (parent instanceof ViewAnalysisParameter) {
            toReturn = 3;
        } else {
            toReturn = 0;
        }
        return toReturn;
    }

    @Override
    public boolean isLeaf(Object node) {
        return !(node instanceof ViewAnalysis) && !(node instanceof ViewAnalysisParameter);
    }

    @Override
    public void valueForPathChanged(TreePath path, Object newValue) {
//        System.out.println(" " + path.getLastPathComponent().getClass());
        if (path.getLastPathComponent() instanceof ViewAnalysis) {
            ViewAnalysis va = (ViewAnalysis) path.getLastPathComponent();
            va.setNameRu(newValue.toString());
        }
        if (path.getLastPathComponent() instanceof ViewAnalysisParameter) {
            ViewAnalysisParameter va = (ViewAnalysisParameter) path.getLastPathComponent();
            va.setNameRu(newValue.toString());
        }
        if (path.getLastPathComponent() instanceof ViewUnits) {
            ViewUnits va = (ViewUnits) path.getLastPathComponent();
            va.setNameRu(newValue.toString());
        }

        if (path.getLastPathComponent() instanceof ViewSize) {
            ViewSize va = (ViewSize) path.getLastPathComponent();
            va.setMaxMinValue(newValue.toString());
        }
    }

    @Override
    public int getIndexOfChild(Object parent, Object child) {
//        System.out.println("works");
        ViewAnalysis va = (ViewAnalysis) parent;
        ViewAnalysisParameter vap = (ViewAnalysisParameter) child;
        return va.getParameters().indexOf(vap);
    }

    @Override
    public void addTreeModelListener(TreeModelListener l) {
        treeModelListeners.addElement(l);
    }

    @Override
    public void removeTreeModelListener(TreeModelListener l) {
        treeModelListeners.removeElement(l);
    }

}
